﻿using Furion;
using Microsoft.AspNetCore.Http;
using PrivateTracker.Service.Core;
using System.ComponentModel.DataAnnotations;
using UAParser;

namespace PrivateTracker.Service.Application;
public class AnnounceInput
{
    private string _info_hash;
    /// <summary>
    /// 种子唯一标识
    /// </summary>
    [Required(ErrorMessage = "不能为空")]
    public string Info_Hash
    {
        get
        {
            return _info_hash;
        }
        set
        {
            if (!string.IsNullOrWhiteSpace(value))
            {
                _info_hash = value.GetHexString();
            }
            else
            {
                _info_hash = value;
            }
        }
    }
    private string _peer_Id;
    /// <summary>
    /// 客户端唯一标识
    /// </summary>
    [Required(ErrorMessage = "不能为空")]
    public string Peer_Id
    {
        get
        {
            return _peer_Id;
        }
        set
        {
            if (!string.IsNullOrWhiteSpace(value))
            {
                _peer_Id = value.GetHexString();
            }
            else
            {
                _peer_Id = value;
            }
        }
    }
    private string _ip;
    /// <summary>
    /// 客户端IP
    /// </summary>
    public string Ip { get; set; }

    private string _ipv6;
    /// <summary>
    /// Ipv6
    /// </summary>
    public string Ipv6 { get; set; }
    /// <summary>
    /// 端口 
    /// </summary>
    [Required(ErrorMessage = "不能为空")]
    public ushort Port { get; set; }
    /// <summary>
    /// 已上传
    /// </summary>
    [Required(ErrorMessage = "不能为空")]
    public long Uploaded { get; set; }
    /// <summary>
    /// 已下载量
    /// </summary>
    [Required(ErrorMessage = "不能为空")]
    public long Downloaded { get; set; }
    /// <summary>
    /// 剩余下载量
    /// </summary>
    [Required(ErrorMessage = "不能为空")]
    public long Left { get; set; }

    /// <summary>
    /// 
    /// </summary>
    public int Corrupt { get; set; }
    /// <summary>
    /// 客户端事件
    /// </summary>
    [Required(ErrorMessage = "不能为空")]
    public PeerEvent Event { get; set; }
    private int? _numwant;
    /// <summary>
    /// 需要的peer的数量
    /// </summary>
    public int? Numwant
    {
        get { return _numwant ?? 50; }//TODO 最终设置全局参数来自后台设置
        set { _numwant = value; }
    }
    /// <summary>
    /// 是否紧凑模式
    /// </summary>
    [Required(ErrorMessage = "不能为空")]
    public int Compact { get; set; }
    /// <summary>
    /// 个人密钥
    /// </summary>
    [Required(ErrorMessage = "密钥不能为空")]
    public string PassKey { get; set; }

    /// <summary>
    /// 获取ipv4
    /// </summary>
    /// <returns></returns>
    public string GetIPv4()
    {
        if (string.IsNullOrWhiteSpace(this._ip))
        {
            this._ip = string.IsNullOrWhiteSpace(Ip) ? App.GetService<IHttpContextAccessor>().HttpContext.GetRequestIPv4() : Ip;
        }
        else
        {
            this._ip = this.Ip;
        }
        return _ip;
    }

    private string _agent;
    /// <summary>
    /// 获取ipv6
    /// </summary>
    /// <returns></returns>
    public string GetIPv6()
    {
        if (string.IsNullOrWhiteSpace(this._ipv6))
        {
            //TODO 服务器上无公网IPv6 时获取到的是本地服务器内网IPv6 暂时搁置
            //  this._ipv6 = string.IsNullOrWhiteSpace(Ip) ? App.GetService<IHttpContextAccessor>().HttpContext.GetRemoteIpAddressToIPv6() : Ipv6;
        }
        else
        {
            this._ipv6 = Ipv6;
        }
        return _ipv6;
    }
    /// <summary>
    /// 获取agent
    /// </summary>
    /// <returns></returns>
    public string GetAgent()
    {
        if (string.IsNullOrWhiteSpace(_agent))
        {
            _agent = Parser.GetDefault().Parse(App.GetService<IHttpContextAccessor>().HttpContext.Request.Headers["User-Agent"]).String;
        }
        return this._agent;
    }
}